package io.github.wslxm.springbootplus2.manage.sys.dao.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import io.github.wslxm.springbootplus2.client.sys.model.vo.USysDictVO;
import io.github.wslxm.springbootplus2.core.base.service.impl.BaseServiceImpl;
import io.github.wslxm.springbootplus2.core.utils.XjBeanUtil;
import io.github.wslxm.springbootplus2.core.utils.XjStringUtil;
import io.github.wslxm.springbootplus2.manage.sys.dao.SysDictDao;
import io.github.wslxm.springbootplus2.manage.sys.mapper.SysDictMapper;
import io.github.wslxm.springbootplus2.manage.sys.model.entity.SysDict;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author wangsong
 */
@Service
public class SysDictDaoImpl extends BaseServiceImpl<SysDictMapper, SysDict> implements SysDictDao {


    @Override
    public List<USysDictVO> findByCode(String code) {
        if (XjStringUtil.isInteger(code)) {
            return null;
        }
        // code 查询
        SysDict dictionary = this.getOne(new LambdaQueryWrapper<SysDict>().eq(SysDict::getCode, code));
        if (dictionary == null) {
            return null;
        }
        List<SysDict> dictList = this.list(new LambdaQueryWrapper<SysDict>()
                .eq(SysDict::getPid, dictionary.getId())
        );
        return XjBeanUtil.listVo(dictList, USysDictVO.class);
    }

    @Override
    public Map<String, USysDictVO> findMapByCode(String code) {
        List<USysDictVO> dictList = findByCode(code);
        if (dictList == null) {
            return null;
        } else {
            return dictList.stream().collect(Collectors.toMap(USysDictVO::getCode, p -> p));
        }
    }

    @Override
    public USysDictVO findByCodeNext(String code, String nextCode) {
        List<USysDictVO> dictCategory = this.findByCode(code);
        if (dictCategory == null || dictCategory.isEmpty()) {
            return null;
        }
        Map<String, USysDictVO> dictMap = dictCategory.stream().collect(Collectors.toMap(USysDictVO::getCode, p -> p));
        return dictMap.get(nextCode);
    }


}
